﻿using Newtonsoft.Json.Linq;

namespace Newtonsoft.Json
{
    /// <summary>字符串去掉首尾空格</summary>
    public class StringTrimConverter : JsonConverter<string>
    {
        public override string ReadJson(JsonReader reader, Type objectType, string existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            var jt = JToken.ReadFrom(reader);
            if (jt.Type.ToString().Equals(nameof(String)))
            {
                return jt.Value<string>().Trim();
            }
            return string.Empty;
        }

        public override void WriteJson(JsonWriter writer, string value, JsonSerializer serializer)
        {
            serializer.Serialize(writer, value.Trim());
        }
    }
}
